# -*- codeing = utf-8 -*-
from selenium import webdriver
import time
import csv
#专门负责定位
f = open('./淘宝商品.csv','w',encoding='utf-8',newline='')
mywrite = csv.writer(f)
mywrite.writerow(['商品名','价格'])

print('*' * 200)
from selenium.webdriver.common.by import By
#Keys调用键盘事件
from selenium.webdriver.common.keys import Keys
import getpass
#等待
from selenium.webdriver.support.wait import WebDriverWait
#期望
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
# 避免终端下执行代码报错
options.add_experimental_option("excludeSwitches", ['enable-automation',
'enable-logging'])
#不加载图片
#options.add_argument('blink-settings=imagesEnabled=false')
#创建浏览器对象
browser = webdriver.Chrome('./chromedriver.exe',options=options)
browser.maximize_window()
URL = "https://www.taobao.com/"
browser.get(url=URL)
# print(browser.window_handles)
# #打开新的选项卡
# browser.execute_script('window.open()')
# print(browser.window_handles)
#
# #切换选项卡,switch_to切换功能
# browser.switch_to.window(browser.window_handles[1])
# browser.get('https://www.baidu.com')
#点击右边登录按钮
browser.find_element(
    By.CSS_SELECTOR,
    'body > div.screen-outer.clearfix > '
    'div.col-right > div.tbh-member.J_Module > div > div.member-ft > div.member-logout.J_MemberLogout > a.btn-login.ml1.tb-bg.weight').click()

browser.switch_to.window(browser.window_handles[1])
#防止检测
# 防止selenium被监测
# 先修改js，再加载js
browser.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{
"source": "Object.defineProperty(navigator, 'webdriver', {get: () =>undefined})"
}
)
#显示等待，在指定时间内出现等待的元素，等待结束，如果未出现，超时异常。
WebDriverWait(browser,5).until(
    EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#login-form > div.fm-btn > button'),'登录')
)
#寻找账号输入框
number  = '199xxxxx2513'
password = 'lxxxxxxxxxxxx'
#添加账号
browser.find_element(By.CSS_SELECTOR,'#fm-login-id').send_keys(number)
#添加密码
browser.find_element(By.CSS_SELECTOR,'#fm-login-password').send_keys(password)
#登录


browser.find_element(By.CSS_SELECTOR,'#login-form > div.fm-btn > button').click()
#跳过滑块检测

browser.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{
"source": "Object.defineProperty(navigator, 'webdriver', {get: () =>undefined})"
}
)
time.sleep(5)

goods = input("请输入商品名")
#在搜索框输入商品
#send_keys,向浏览器定位的某个元素发送信息。（向输入框中输入内容、按键事件等）
browser.find_element(By.CSS_SELECTOR,'#q').send_keys(goods)

#方法一：搜索框直接回车
#直接回车
#browser.find_element(By.CSS_SELECTOR,'#q').send_keys(Keys.ENTER)
#方法二：点击按钮

browser.find_element(By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button').click()

WebDriverWait(browser, 5).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#J_SearchForm > button'), '搜索')
    )
#向下刷新界面，拉取界面。
y = 0
y_max = 7000
while y < y_max:
    y += 500
    #调用js实现页面滚动刷新
    browser.execute_script(f'window.scrollTo(0,{y})')
    WebDriverWait(browser, 5).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#J_SearchForm > button'), '搜索')
    )
#保存所有手机的列表
list_div = browser.find_elements(By.CSS_SELECTOR,'#mainsrp-itemlist > div > div > div:nth-child(1) > div')
for i in list_div:
    title = i.find_element(By.CSS_SELECTOR,'div > div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-2 > a').text
    price = i.find_element(By.CSS_SELECTOR,'div > div.ctx-box.J_MouseEneterLeave.J_IconMoreNew > div.row.row-1.g-clearfix > div.price.g_price.g_price-highlight').text
    print(title,price)
    mywrite.writerow([title,price])
#print(list_div,len(list_div))
f.close()

